package com.kk.miaosha.event;

import com.kk.miaosha.utils.MqConstantUtil;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

/**
 * @author : K k
 * @date : 21:07 2020/9/26
 */
@Component
public class EventProducer {

    @Autowired
    RabbitTemplate rabbitTemplate;

    //处理事件
    public void send(Map<String,Object> map){
        //将事件发布到指定的主题
        rabbitTemplate.convertAndSend(MqConstantUtil.DELAY_QUEUE, map, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration("50000");//20秒
                return message;
            }
        });
        System.out.println("----发送消息到队列中------:"+new Date());
    }
}
